<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/18
 * Time: 14:41
 */

namespace app\api\controller;

use think\Db;
use org\Qiniu;

class Oms extends Base
{
    public function aaa(){
        $bodyData = [
            'sku' => 'KWTBL1G07000214'
        ];
        $result = $this->curl_oms('stock.list.get',json_encode($bodyData));
        dump($result);die;
    }
    // public function initColorSize()
    // {
        // set_time_limit(0);
        // //一个颜色
        // $num = 0;
        // $bodyData = [
            // 'color_code' => '2022',
            // 'color_name' => '通用',
            // 'color_note' => 'APP'
        // ];
        // $res = $this->curl_oms('colors.add',json_encode($bodyData));
        // if($res['status'] == 'api-success') $num += 1;
        // //N个尺码
        // $sizeArr = [
            // '000','100','110','120','130','140','150','155','160','165','170',
            // '175','180','185','190','195','200','205','210','215','220'
        // ];
        // foreach ($sizeArr as $k => $v){
            // $bodyData = [
                // 'size_code' => $v,
                // 'size_name' => $v
            // ];
            // $res = $this->curl_oms('sizes.add',json_encode($bodyData));
            // if($res['status'] == 'api-success') $num += 1;
        // }
        // exit($num);
    // }
    // public function initGoodsAndSku()
    // {
        // set_time_limit(0);
        // $data = Db::table('up_self_goods')
            // ->where('good_status','<>',3)
            // ->where('good_oms',1)
            // ->field('good_id,good_name,good_coding,good_price,good_status')
            // ->select();
        // if($data){
            // foreach ($data as $k => $v){
                // $newCoding = substr(trim($v['good_coding'],' '),0,13);
                // $bodyData = [
                    // 'goods_sn' => $newCoding,
                    // 'goods_name' => $v['good_name'],
                    // 'goods_id' => $v['good_id'],
                    // 'market_price' => $v['good_price'],
                    // 'bzsj' => $v['good_price'],
                    // 'sp' => 1,
                    // 'sprq' => date('Y-m-d'),
                    // 'is_combo' => 0,
                    // 'goodscj' => 0,
                    // 'enable' => 1
                // ];
                // $addres = $this->curl_oms('products.add',json_encode($bodyData));
                // if($addres['status'] != 'api-success') exit('同步商品错误：' . $v['good_id']);
                // Db::table('up_self_goods')->where('good_id',$v['good_id'])->setField('good_oms',2);
                // $details = Db::table('up_self_goods_attr')
                    // ->where('goods_id',$v['good_id'])
                    // ->where('is_delete',0)
                    // ->field('attr_id,sign_str')
                    // ->select();
                // if($details){
                    // foreach ($details as $k2 => $v2){
                        // $skuData = [
                            // 'goods_sn' => $newCoding,
                            // 'color_code' => '2022',
                            // 'size_code' => $v2['sign_str']
                        // ];
                        // $skuData['sku'] = $newCoding . $skuData['size_code'];
                        // $addres2 = $this->curl_oms('skus.add',json_encode($skuData));
                        // if($addres2['status'] != 'api-success') exit('同步sku错误：' . $v2['attr_id']);
                    // }
                // }
            // }

        // }
        // exit();
    // }
    public function getSkuStockOms()
    {
        set_time_limit(0);
        $sizeU8 = [
            '000' => 'A1',
            '100' => 10,
            '110' => 12,
            '120' => 14,
            '130' => 16,
            '140' => 18,
            '150' => 20,
            '155' => 22,
            '160' => 24,
            '165' => 26,
            '170' => 28,
            '175' => 30,
            '180' => 32,
            '185' => 34,
            '190' => 36,
            '195' => 38,
            '200' => 40,
            '205' => 42,
            '210' => 44,
            '215' => 46,
            '220' => 48,
            '225' => 50,
        ];
        $hour = time();
        $data = Db::table('up_self_goods_attr')->alias('attr')
            ->join('up_self_goods g','attr.goods_id=g.good_id','LEFT')
            ->where('is_delete',0)
            ->where('good_status','in','1,2')
            ->order('oms_get asc')
            ->field('good_coding,attr_id,sign_str,stock')
            ->page(1,100)
            ->select();
//        dump($data);die;
        if($data){
            foreach ($data as $k => $v){
                if(isset($sizeU8[$v['sign_str']]) && $sizeU8[$v['sign_str']]) $v['sign_str'] = $sizeU8[$v['sign_str']];
                $newCoding = substr(trim($v['good_coding'],' '),0,13);
                $bodyData = [
                    'sku' => $newCoding . $v['sign_str'],
                    'ckCode' => '074'
                ];
                $result = $this->curl_oms('stock.list.get',json_encode($bodyData));
                if (isset($result['status']) && $result['status'] == 'api-success') {
                    if(isset($result['data']['stockListGet'][0]['kysl']) && $result['data']['stockListGet'][0]['kysl']){
                        Db::table('up_self_goods_attr')
                            ->where('attr_id', $v['attr_id'])
                            ->update([
                                'stock' => $result['data']['stockListGet'][0]['kysl'],
                                'oms_get' => $hour
                            ]);
                    }else{
                        Db::table('up_self_goods_attr')
                            ->where('attr_id', $v['attr_id'])
                            ->update([
                                'stock' => 0,
                                'oms_get' => $hour
                            ]);
                    }
                }
                Db::table('up_self_goods_attr')
                    ->where('attr_id', $v['attr_id'])
                    ->update([
                        'oms_get' => $hour
                    ]);
            }
        }
        exit();
    }
    /**
     * 8小时之后再推送给oms
     */
    public function omsZiyingHours()
    {
        set_time_limit(0);
        $today = date('Y-m-d H:i:s');
        $time = time();
        $hours = Db::table('up_about')->where('id', 15)->value('content');
//        $hours = (int)$hours;
        $hours_ago = date("Y-m-d H:i:s", ($time - 3600 * $hours));
        $where['is_pay'] = 1;
        $where['order_contract'] = 1;
        $where['is_delete'] = 0;
        $where['is_send'] = 0;
        $where['order_push_erp'] = 1;
        $where['order_cancel_status'] = ['<=', 2];
        $where['created_at'] = ['>', '2022-11-09 00:00:00'];
        $where['pay_time'] = ['<=', $hours_ago];
        $field = 'order_no,order_id,name,mobile,address,order_address2,total_price,all_goods_price,express_original_price,order_contract';
        $field .= ',order_erp_year,order_erp_done,order_back_order_id,created_at,remark,pay_type';
        $datas = Db::table('up_self_order')
            ->where($where)
            ->field($field)
            ->page(1,50)
            ->select();
//        dump($datas);die;
        if ($datas) {
            foreach ($datas as $k => $v) {
                $user = explode('|||', $v['order_address2']);
                $master = [
                    'add_time' => $v['created_at'],
                    'order_sn' => $v['order_no'],
                    'sd_code' => 'PX003', //店铺代码：cs001
                    'order_status' => 1,
                    'pay_status' => 2,
                    'consignee' => $v['name'],
                    'province_name' => $user[0],
                    'city_name' => $user[1],
                    'district_name' => $user[2],
                    'address' => $user[3],
                    'email' => $v['order_back_order_id'],
                    'mobile' => $v['mobile'],
                    'zipcode' => '0',
                    'pos_code' => '', //oto门店代码
                    'pay_code' => $v['pay_type'] == 1 ? 'weixin' : 'alipay',
                    'vip_no' => '', //会员编号
                    'shipping_code' => '', //快递公司代码
                    'shipping_fee' => $v['express_original_price'] ?: 0,
                    'order_amount' => $v['total_price'],
                    'payment' => $v['total_price'],
                    'receiver_tel' => $v['mobile'],
                    'receiver_zip' => '', //收货邮政编码
                    'os_user_id' => '', //分销商账号
                    'inv_kind' => '0', //开票类型
                    'inv_bank' => '', //
                    'inv_account' => '', //
                    'inv_addr' => '', //
                    'inv_phone' => '', //
                    'inv_count' => '', //
                    'inv_number' => '', //
                    'seller_msg' => '', //商家备注
                    'storage_message' => '', //仓库留言
                    'order_msg' => $v['remark'],
                    'postscript' => $v['remark']
                ];
                $field = 'goods_coding_unique sku_sn,unit_price goods_price,unit_price transaction_price,num goods_number,cast(tg_type as char) Is_yugou';
                $details = Db::table('up_self_order_detail')
                    ->where('order_id', $v['order_id'])
                    ->field($field)
                    ->select();
                $master['items'] = $details;
//                dump($master);die;
                $result = $this->curl_oms('order.detail.add', json_encode($master));
                if (isset($result['status']) && $result['status'] == 'api-success') {
                    Db::table('up_self_order')
                        ->where('order_id', $v['order_id'])
                        ->update(['order_push_erp' => 2,'new_erp_have' => 2]);
                }else{
                    if(isset($result['data'][0]['message']) && $result['data'][0]['message']){
                        $string = $result['data'][0]['message'];
                        if(strpos($string,'交易号') !== false && strpos($string,'在系统中已存在') !== false){
                            Db::table('up_self_order')
                                ->where('order_id', $v['order_id'])
                                ->update(['order_push_erp' => 2,'new_erp_have' => 2]);
                        }else{
                            Db::table('up_zzzz_oms')->insert([
                                'nono' => $v['order_id'] . '||' . $v['order_no'],
                                'content' => json_encode($result),
                                'time' => $today
                            ]);
                        }
                    }else{
                        Db::table('up_zzzz_oms')->insert([
                            'nono' => $v['order_id'] . '||' . $v['order_no'],
                            'content' => json_encode($result),
                            'time' => $today
                        ]);
                    }
                    dump($result);
                }
            }
        }
    }

    /**
     * 同步物流信息
     */
    public function getOmsWuliu()
    {
        //模拟erp返回物流信息时改订单状态
        set_time_limit(0);
        $hour = time();
        $today = date('Y-m-d H:i:s');
        $where['is_pay'] = 1;
        $where['order_contract'] = 1;
        $where['is_delete'] = 0;
        $where['is_send'] = 0;
        $where['order_push_erp'] = 2;
        $where['order_cancel_status'] = ['<=', 2];
//        $where['oms_count'] = ['<>', $hour];
        $field = 'order_no,order_id,is_send';
        $datas = Db::table('up_self_order')
            ->where($where)
            ->field($field)
            ->order('oms_count asc')
            ->page(1,10)
            ->select();
//        dump($datas);die;
        if ($datas) {
            Db::startTrans();
            try {
                foreach($datas as $k => $v){
                    $master = [
                        'order_sn' => $v['order_no']
                    ];
                    $result = $this->curl_oms('order.detail.get', json_encode($master));
                    if (isset($result['status']) && $result['status'] == 'api-success') {
                        if(isset($result['data']['shipping_status']) && ($result['data']['shipping_status'] == 6 || $result['data']['shipping_status'] == 7)){
                            Db::table('up_self_order')
                                ->where('order_id', $v['order_id'])
                                ->update([
                                    'is_send' => 1,
                                    'send_time' => $today,
                                    'updated_at' => $today,
                                    'express' => $result['data']['shipping_code'],
                                    'express_no' => $result['data']['shipping_sn'],
                                ]);
                            Db::table('up_self_order_detail')
                                ->where('order_id', $v['order_id'])
                                ->update([
                                    'updated_at' => $today,
                                    'detail_express' => $result['data']['shipping_code'],
                                    'detail_express_no' => $result['data']['shipping_sn'],
                                ]);
                        }
                    }
                    //不管查询成功与否，都跳过，不影响下面的数据
                    Db::table('up_self_order')->where('order_id', $v['order_id'])->setField('oms_count',$hour);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
            }
            exit();
        }else{
            exit();
        }
    }
    /**
     * 未发货之前取消订单的，发给erp
     */
    public function cancelOrderOms()
    {
        
        set_time_limit(0);
        $today = date('Y-m-d H:i:s');
        $time = time();
        $where = [];
        $where['is_pay'] = 2;
        $where['order_contract'] = 1;
        $where['is_delete'] = 0;
        $where['order_push_erp'] = 2; //没推过的不用
        $where['order_cancel_push_erp'] = 1;
        $where['order_cancel_status'] = 3;
        $field = 'order_no,order_id,all_goods_price,express_original_price,order_contract';
        $field .= ',order_cancel_number,order_cancel_time,order_cancel_remark';
        $datas = Db::table('up_self_order')
            ->where($where)
            ->field($field)
            ->page(1,10)
            ->select();
//        dump($datas);die;
        if ($datas) {
            foreach ($datas as $k => $v) {
                //没发货要调用取消订单的接口
                $master = [
                    'order_sn' => '',
                    'deal_code' => $v['order_no'],
                    'ly_type' => 'openshop'
                ];
//                dump($master);die;
                $result = $this->curl_oms('order.cancel.delivery', json_encode($master));
                if (isset($result['status']) && $result['status'] == 'api-success') {
                    Db::table('up_self_order')
                        ->where('order_id', $v['order_id'])
                        ->update(['order_cancel_push_erp' => 2]);
                    //
                    $master = [
                        'order_sn' => '',
                        'deal_code' => $v['order_no']
                    ];
                    $this->curl_oms('order.zwx', json_encode($master));
                }else if(isset($result['data'][0]['message']) && $result['data'][0]['message'] == '订单已完成，不能取消操作！') {
                    Db::table('up_self_order')
                        ->where('order_id', $v['order_id'])
                        ->update(['order_cancel_push_erp' => 2]);
                }else{
                    dump($result);
                }
            }
        }
        //2、售后的推不管退还是换，先推个售后（不在这推了，在回寄物流那里推）
//        $where2 = [];
//        $where2['tui_type'] = 1;
//        $where2['status'] = 2;
//        $where2['erp_push'] = 1;
//
//        $field = 'o.order_no,o.order_id,all_goods_price,total_price,express_original_price,o.name,o.mobile,o.address,o.order_address2,o.order_back_order_id';
//        $field .= ',t.type,t.order_erp_wl_no,o.pay_type';
//        $field .= ',t.express,t.express_no,t.remark,t.created_at,t.status_time';
//
//        $datas = Db::table('up_self_order_tui')->alias('t')
//            ->join('up_self_order o', 't.order_id=o.order_id', 'LEFT')
//            ->where($where2)
//            ->where('new_erp_have',2) //必须是app的推给过oms的单;
//            ->field($field)
//            ->group('order_erp_wl_no')
//            ->page(1,10)
//            ->select();
////        dump($datas);die;
//        if ($datas) {
//            foreach ($datas as $k => $v) {
//                //A:先推退货：跟erp不同，这里要给出原先的订单单号和要退的原尺码的衣服明细
//                $youhua_status = false;
//                $master = [];
//                $master['sell_return_record'] = [
//                    'order_sn' => $v['order_no'],
//                    'refund_code' => $v['order_erp_wl_no'] . 'tapp',
//                    'return_ck_code' => '', // 退货仓库代码
//                    'return_kw_code' => '', // 退货库位代码
//                    'return_shipping_sn' => $v['express_no'],
//                    'shipping_sn' => $v['express_no'],
//                    'return_type' => 1
//                ];
//                $field = 'goods_coding_unique sku,num goods_number,unit_price shop_price';
//                $details = Db::table('up_self_order_tui')->alias('t')
//                    ->join('up_self_order_detail od', 't.order_detail_id=od.detail_id', 'LEFT')
//                    ->where($where2)
//                    ->where('t.order_erp_wl_no', $v['order_erp_wl_no'])
//                    ->field($field)
//                    ->select();
//                $master['sell_return_record']['order_return_goods'] = $details;
////                 dump($master);die;
//                $result = $this->curl_oms('order.return.add', json_encode($master));
//                if (isset($result['status']) && $result['status'] == 'api-success') {
//                    Db::table('up_self_order_tui')
//                        ->where($where2)
//                        ->where('order_erp_wl_no', $v['order_erp_wl_no'])
//                        ->setField('erp_push', 2);
//                }else{
//                    dump($result);
//                }
//                //B:换货的再推新单(不需要了，因为要先确认拿到货再推给oms)
//            }
//        }
    }
    /**
     * 同步售后的是否收到货的状态
     */
    public function getOmsSaleGet()
    {
        //模拟erp返回物流信息时改订单状态
        set_time_limit(0);
        $today = date('Y-m-d H:i:s');
        $hour = time();
        //2、售后的推不管退还是换，先推个售后
        $where2 = [];
        $where2['tui_type'] = 1;
        $where2['status'] = 2;
        $where2['erp_push'] = 2;
        $where2['tui_sure_get'] = 1;
//        $where2['tui_oms_count'] = ['<>',$hour];

        $field = 'o.order_no,o.order_id,o.user_id,o.order_contract,all_goods_price,total_price,express_original_price,o.name,o.mobile,o.address,o.order_address2,o.order_back_order_id';
        $field .= ',t.type,t.tui_id,t.order_erp_wl_no,o.pay_type';
        $field .= ',t.express,t.express_no,t.remark,t.created_at,t.status_time';

        $datas = Db::table('up_self_order_tui')->alias('t')
            ->join('up_self_order o', 't.order_id=o.order_id', 'LEFT')
            ->where($where2)
            ->field($field)
            ->group('order_erp_wl_no')
            ->order('tui_oms_count asc')
            ->page(1,10)
            ->select();
//        dump($datas);die;
        if ($datas) {
            foreach ($datas as $k => $v) {
                //先确定有没有收到货
                $master = [
                    'refund_code' => $v['order_erp_wl_no'] . 'tapp'
                ];
                $result = $this->curl_oms('return.detail.get', json_encode($master));
//                dump($result);die;
                if (isset($result['status']) && $result['status'] == 'api-success'){
                    Db::startTrans();
                    try {
                        if(isset($result['data'][0]['return_order_status']) && $result['data'][0]['return_order_status'] == 10){
                            Db::table('up_self_order_tui')
                                ->where('status',2)
                                ->where('order_erp_wl_no', $v['order_erp_wl_no'])
                                ->setField('tui_sure_get', 2);
                            //这时候再推个新单（如果更新状态成功了，但是没有推新单，那就查一下确定再手动推）
                            if ($v['type'] == 1) {
                                $user = explode('|||', $v['order_address2']);
                                $master = [
                                    'add_time' => $v['status_time'],
                                    'order_sn' => $v['order_erp_wl_no'],
                                    'sd_code' => 'PX003', //店铺代码
                                    'order_status' => 1,
                                    'pay_status' => 2,
                                    'consignee' => $v['name'],
                                    'province_name' => $user[0],
                                    'city_name' => $user[1],
                                    'district_name' => $user[2],
                                    'address' => $user[3],
                                    'email' => $v['mobile'],
                                    'mobile' => $v['mobile'],
                                    'zipcode' => '0',
                                    'pos_code' => '', //oto门店代码
                                    'pay_code' => $v['pay_type'] == 1 ? 'weixin' : 'alipay',
                                    'vip_no' => '', //会员编号
                                    'shipping_code' => '', //快递公司代码
                                    'shipping_fee' => $v['express_original_price'] ?: 0,
                                    'order_amount' => $v['total_price'],
                                    'payment' => $v['total_price'],
                                    'receiver_tel' => $v['mobile'],
                                    'receiver_zip' => '', //收货邮政编码
                                    'os_user_id' => '', //分销商账号
                                    'inv_kind' => '0', //开票类型
                                    'inv_bank' => '', //
                                    'inv_account' => '', //
                                    'inv_addr' => '', //
                                    'inv_phone' => '', //
                                    'inv_count' => '', //
                                    'inv_number' => '', //
                                    'seller_msg' => '', //商家备注
                                    'storage_message' => '', //仓库留言
                                    'order_msg' => $v['remark'],
                                    'postscript' => $v['remark']
                                ];
                                $field = 'erp_huan_uninumber sku_sn,unit_price goods_price,unit_price transaction_price,num goods_number,cast(tg_type as char) Is_yugou';
                                $details =Db::table('up_self_order_tui')->alias('t')
                                    ->join('up_self_order_detail od', 't.order_detail_id=od.detail_id', 'LEFT')
                                    ->where('status',2)
                                    ->where('t.order_erp_wl_no', $v['order_erp_wl_no'])
                                    ->field($field)
                                    ->select();
                                $master['items'] = $details;
//                dump($master);die;
                                $result = $this->curl_oms('order.detail.add', json_encode($master));
                                if (isset($result['status']) && $result['status'] == 'api-success') {
                                    Db::table('up_self_order_tui')
                                        ->where('status',2)
                                        ->where('order_erp_wl_no', $v['order_erp_wl_no'])
                                        ->setField('erp_push', 3);
                                }else{
                                    dump($result);
                                }
                            }else{
                                $order_erp_wl_no = $v['order_erp_wl_no'];
                                //直接退款给用户
                                $auto = Db::table('up_about')->where('id',21)->value('content');
                                if($auto == 1){
                                    $should_money = Db::table('up_self_order_tui')
                                        ->where('order_erp_wl_no',$order_erp_wl_no)
                                        ->where('status',2)
                                        ->where('is_send',0)
                                        ->sum('should_money');
                                    if($should_money > 0 && $should_money <= $v['all_goods_price']){
                                        $update  = [
                                            'is_send' => 1,
                                            'tui_master_status' => 4,
                                            'refund_price' => $should_money,
                                            'send_time' => $today,
                                            'updated_at' => $today,
                                        ];
                                        $payInfo = [
                                            'total_price' => $v['total_price'],
                                            'money' => $should_money,
                                            'number' => $v['order_no']
                                        ];
                                        if($v['pay_type'] == 1){ //微信
                                            $tuiwx = new \app\api\controller\Tuiwx();
                                            $res = $tuiwx->refundWx($payInfo,$order_erp_wl_no);
                                        }elseif ($v['pay_type'] == 2){ //支付宝
                                            $tuiali = new \app\api\controller\Ali();
                                            $res = $tuiali->refund($payInfo,$order_erp_wl_no);
                                        }else{
                                            //支付方式有问题？不会出现这个情况
                                            $update = [
                                                'merchant_remark' => 'mww-查找无此支付方式'
                                            ];
                                        }
                                        if($res['status'] != 200) {
                                            $update = [
                                                'merchant_remark' => 'mww-退款失败'
                                            ];
                                        }
                                        Db::table('up_self_order_tui')
                                            ->where('order_erp_wl_no',$order_erp_wl_no)
                                            ->where('status',2)
                                            ->where('is_send',0)
                                            ->update($update);
                                        if($res['status'] == 200) {
                                            $jj_deids = Db::table('up_self_order_tui')
                                                ->where('order_erp_wl_no',$order_erp_wl_no)
                                                ->where('status',2)
                                                ->where('is_send',0)
                                                ->column('order_detail_id');
                                            Db::table('up_self_order_detail')
                                                ->where('detail_id', 'in',$jj_deids)
                                                ->setField('refund_status',2);
                                            $order_id = $v['order_id'];
                                            $sale_status = Db::table('up_self_order')
                                                ->where('order_id', $order_id)
                                                ->value('sale_status');
                                            if($sale_status != 0){
                                                Db::table('up_self_order')
                                                    ->where('order_id', $order_id)
                                                    ->setField('tui_status',1);
                                            }
                                            allInRevenue(2,$should_money,$v['user_id'],$v['order_contract'],$v['order_id'],'自动退款',$v['tui_id']);
                                        }
                                    }
                                }
                            }
                        }
                        if(isset($result['data'][0]['return_order_status']) && $result['data'][0]['return_order_status'] == 3){
                            Db::table('up_self_order_tui')
                                ->where('status',2)
                                ->where('order_erp_wl_no', $v['order_erp_wl_no'])
                                ->setField('tui_sure_get', 3);
                        }
                        Db::commit();
                    } catch (\Exception $e) {
                        Db::rollback();
                    }
                }
                //-------
                Db::table('up_self_order_tui')
                    ->where('status',2)
                    ->where('order_erp_wl_no', $v['order_erp_wl_no'])
                    ->setField('tui_oms_count', $hour);
            }
        }
    }
    /**
     * 同步售后订单物流信息
     */
    public function getOmsSaleWuliu()
    {
        //模拟erp返回物流信息时改订单状态
        set_time_limit(0);
        $hour = time();
        $today = date('Y-m-d H:i:s');
        $where2 = [];
        $where2['tui_type'] = 1;
        $where2['type'] = 1;
        $where2['status'] = 2;
        $where2['erp_push'] = 3;
        $where2['tui_sure_get'] = 2;
        $where2['is_send'] = 0;
//        $where2['tui_oms_count2'] = ['<>',$hour];
        $datas = Db::table('up_self_order_tui')
            ->where($where2)
            ->field('order_erp_wl_no,order_id')
            ->group('order_erp_wl_no')
            ->order('tui_oms_count2 asc')
            ->page(1,10)
            ->select();
//        dump($datas);die;
        if ($datas) {
            Db::startTrans();
            try {
                foreach($datas as $k => $v){
                    $master = [
                        'order_sn' => $v['order_erp_wl_no']
                    ];
                    $result = $this->curl_oms('order.detail.get', json_encode($master));
//                    dump($result);die;
                    if (isset($result['status']) && $result['status'] == 'api-success') {
                        if(isset($result['data']['shipping_status']) && ($result['data']['shipping_status'] == 6 || $result['data']['shipping_status'] == 7)){
                            Db::table('up_self_order_tui')
                                ->where('order_erp_wl_no', $v['order_erp_wl_no'])
                                ->where('status', 2)
                                ->update([
                                    'is_send' => 1,
                                    'send_time' => $today,
                                    'updated_at' => $today,
                                    'merchant_express' => $result['data']['shipping_code'],
                                    'merchant_express_no' => $result['data']['shipping_sn'],
                                ]);

                            $jj_deids = Db::table('up_self_order_tui')
                                ->where('order_erp_wl_no', $v['order_erp_wl_no'])
                                ->where('status', 2)
                                ->column('order_detail_id');
                            Db::table('up_self_order_detail')
                                ->where('detail_id', 'in', $jj_deids)
                                ->setField('refund_status', 2);

                            $sale_status = Db::table('up_self_order')
                                ->where('order_id', $v['order_id'])
                                ->value('sale_status');
                            if ($sale_status != 0) {
                                Db::table('up_self_order')
                                    ->where('order_id', $v['order_id'])
                                    ->setField('tui_status', 1);
                            }
                        }
                    }
                    Db::table('up_self_order_tui')
                        ->where('order_erp_wl_no', $v['order_erp_wl_no'])
                        ->where('status', 2)
                        ->setField('tui_oms_count2',$hour);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
            }
            exit();
        }else{
            exit();
        }
    }
    //自营推给oms的退货单
    public function bbb(){
        //2、售后的推不管退还是换，先推个售后
        $order_erp_wl_no = 'app169697435093179136';
        $express = '顺丰';
        $express_no = 'SF1427618378757';
            $where2 = [];
            $where2['tui_type'] = 1;
            $where2['status'] = 2;
//            $where2['erp_push'] = 1;

            $field = 'o.order_no,o.order_id,all_goods_price,total_price,express_original_price,o.name,o.mobile,o.address,o.order_address2,o.order_back_order_id';
            $field .= ',t.type,t.order_erp_wl_no,o.pay_type';
            $field .= ',t.express,t.express_no,t.remark,t.created_at,t.status_time';
            $v = Db::table('up_self_order_tui')->alias('t')
                ->join('up_self_order o', 't.order_id=o.order_id', 'LEFT')
                ->where($where2)
                ->where('order_erp_wl_no',$order_erp_wl_no) //必须是app的推给过oms的单;
                ->field($field)
                ->find();
//            dump($v);die;
            if ($v){
                $master = [];
                $master['sell_return_record'] = [
                    'order_sn' => $v['order_no'],
                    'refund_code' => $v['order_erp_wl_no'] . 'tapp',
                    'return_ck_code' => '', // 退货仓库代码
                    'return_kw_code' => '', // 退货库位代码
                    'return_shipping_sn' => $express_no,
                    'shipping_name' => $express,
                    'shipping_sn' => $express_no,
                    'return_type' => 1
                ];
                $field = 'od.detail_id sku,num goods_number,unit_price shop_price';
                $details = Db::table('up_self_order_tui')->alias('t')
                    ->join('up_self_order_detail od', 't.order_detail_id=od.detail_id', 'LEFT')
                    ->where($where2)
                    ->where('t.order_erp_wl_no', $v['order_erp_wl_no'])
                    ->field($field)
                    ->select();
                foreach ($details as $k222 => $v222){
                    $oldSD = $this->getLastInfo($v222['sku'],$v['type']);
                    $details[$k222]['sku'] = $oldSD['erp_huan_uninumber'];
                }
                if(isset($oldSD['order_erp_wl_no']) && $oldSD['order_erp_wl_no']){
                    $master['sell_return_record']['order_sn'] = $oldSD['order_erp_wl_no']; // 确保单号正确
                }
                $master['sell_return_record']['order_return_goods'] = $details;
                dump($master);die;
                $result = $this->curl_oms('order.return.add', json_encode($master));
//                Db::table('up_zzzz_oms')->insert([
//                    'nono' => 'oid=>' . $order_id,
//                    'content' => json_encode($result),
//                    'time' => date('Y-m-d H:i:s')
//                ]);
                if (isset($result['status']) && $result['status'] == 'api-success') {
                    $res = Db::table('up_self_order_tui')
                        ->where($where2)
                        ->where('order_erp_wl_no', $v['order_erp_wl_no'])
                        ->setField('erp_push', 2);
                    dump($res);
                    exit();
                }else{
                    dump($result);
                    exit('error！');
                }
            }
    }
}